<?php
class CLogin {
	
	static function show()
	{
		global $pageContent, $page, $user;
		
                $xtpl = new XTemplate('home.html',PAGE_PATH.DS.DS.$page.DS.DS.'tpl');
		if (isset($_POST['submit'])) {
			$username = trim( $_POST['username'] );
			$password = trim( $_POST['password'] );                        
			if ($username && $password) {                            
				if ($userid = $user->login($username, $password)) {                                    
                                    $info = $user->get_user_info($userid);
                                    foreach ($info as $key => $value){
                                        
                                        $_SESSION[$key]=$value;
                                    }
                                    $xtpl->assign("MSG", "Successfull !");
                                    redirect(ABS_URL);
				}else {
                                        //echo "hi";
					$xtpl->assign("MSG", '<div id="warning" style="display:block;width:300px;"><p>Invalid username or password!</p></div>');
				}
			}else {
                                //echo "hello";
				$xtpl->assign('MSG' , '<div id="warning"  style="display:block;width:300px"><p>Invalid username or password!</p></div>');
			}

                    $xtpl->parse('main');
                    $pageContent= $xtpl->text('main');
		}else{
                    if (!isset($_SESSION["username"])){
                        
                        $xtpl->parse('main');
                        $pageContent= $xtpl->text('main');
                    }else{
                        
                        redirect(ABS_URL);
                    }
                }
	}
	
	static function logout()
	{
		global $pageContent, $page;
		$_SESSION["username"] = null;
		session_destroy();
                redirect(ABS_URL."?page=sign");
	}
	
}
?>